from selenium.common.exceptions import TimeoutException
import time
import random
from selenium import webdriver
import selenium
from collections import OrderedDict
import platform
Driver_path = './chromedriver.exe'
if platform.system() == 'Linux':
    Driver_path = './chromedriver'

# ai_kw = ['人工智能','机器学习','深度学习','强化学习','智能机器人','聊天机器人','虚拟助理','计算机视觉','图像识别','人脸识别','文字识别','可视化搜索','指纹识别','虹膜识别','手势控制','自然语言理解','自然语言处理','文本分析','语音识别','语义网络','语义搜索','无人车','无人驾驶','自动驾驶','辅助驾驶','自动泊车','智能投顾','神经计算','神经网络','算法博弈论','知识工程','知识表示','知识图','逻辑编程','规则基系统','规则引擎','进化计算','认知计算','计算智能','超级智能','自动推理','模糊逻辑','智能代理','感知计算','Artificial intelligence','Machine learning','Deep learning','Reinforcement learning','Robots','Chatterbot','Virtual Assistant','Computer Vision','Image Recognition','Facial Recognition','text recognition','Visual Search','fingerprint recognition','Iris recognition','Gesture control','Natural language understanding','Natural language processing','Text Analytics','Voice Recognition','Semantic Web','Semantic Search','Autonomous vehicle','autopilot','self driving','ADAS','Automatic parking','robo-adviser','Neurocomputing','Neural network','计算博弈论','基于规则系统','感知计算','智能主体','知觉计算','Robotics','chat bot','face recognition','finger print recognition','NLP','TEXT ANALYSIS','Autonomous car','auto pilot','self-driving','Advanced driver assistance system','robo-advisor','Neurocomput','基于规则的系统','智能体','Robotic','Autonomous cars','auto-pilot','self drive','Advanced driver assistance system','robo adviser','Algorithmic game theory','Knowledge engineering','Knowledge representation','Knowledge graph','Logic programming','Rule-based system','Rule engine','Evolutionary computation','Cognitive computing','Computational intelligence','Superintelligence','Automated reasoning','Fuzzy logic','Intelligent agent','Perceptual computing','Autonomous vehicles','self-drive','driver assistance systems','robo advisor','Algorithmic game theor','Rule based system','Perceptual comput','driver assistance system',]
ai_kw = ['感知计算','Artificial intelligence','Machine learning','Deep learning','Reinforcement learning','Robots','Chatterbot','Virtual Assistant','Computer Vision','Image Recognition','Facial Recognition','text recognition','Visual Search','fingerprint recognition','Iris recognition','Gesture control','Natural language understanding','Natural language processing','Text Analytics','Voice Recognition','Semantic Web','Semantic Search','Autonomous vehicle','autopilot','self driving','ADAS','Automatic parking','robo-adviser','Neurocomputing','Neural network','计算博弈论','基于规则系统','感知计算','智能主体','知觉计算','Robotics','chat bot','face recognition','finger print recognition','NLP','TEXT ANALYSIS','Autonomous car','auto pilot','self-driving','Advanced driver assistance system','robo-advisor','Neurocomput','基于规则的系统','智能体','Robotic','Autonomous cars','auto-pilot','self drive','Advanced driver assistance system','robo adviser','Algorithmic game theory','Knowledge engineering','Knowledge representation','Knowledge graph','Logic programming','Rule-based system','Rule engine','Evolutionary computation','Cognitive computing','Computational intelligence','Superintelligence','Automated reasoning','Fuzzy logic','Intelligent agent','Perceptual computing','Autonomous vehicles','self-drive','driver assistance systems','robo advisor','Algorithmic game theor','Rule based system','Perceptual comput','driver assistance system',]
# setInterval("jQuery('.next').click()",5000); 
cookies = 'JSESSIONID="ajax:7752026804203399614"; bcookie="v=2&4650b692-18ed-4049-8bdb-9c1c4fffc35f"; bscookie="v=1&201707250439409deb0aea-3b97-43a2-838b-1d4feb27909eAQH_C8Aa0xpyZc5l70zk5nNwLEwAd3fH"; lidc="b=VGST08:g=394:u=1:i=1501052095:t=1501138495:s=AQE_v1uo8AKGgSO_x34FVus1emzma-wZ"; visit="v=1&M"; _lipt=CwEAAAFdgq3iz_Qz2V_iL8JXlvNz97W3k-gJeGV17Lu0evCeAkstol_xteWiRNe85q9hqWmv6rmaxP8w88zyGedF-KEsKQPPN3xTfvBq96D9Y5FpSdhV_KLwPp9v3b4K; _ga=GA1.2.1444402639.1501032016; lang="v=2&lang=en-us"; liap=true; sl="v=1&n1M0h"; li_at=AQEDASNGaHQFRfV3AAABXX2tSoMAAAFdgsyx7k0Ac2XeBellauh8RxF-g3hpv5SFKMLa__G4sdafgpZu24XIEYf_PhpHVYcTUaJNS3kdLYW8Gq8QhAzru86H-nfK-rZW3rpk9zju351XBr8tm6fwyCvY'


def parse_cookie(cookies):
    cookielist = cookies.split("; ")
    cookies = []
    cookies = []
    for sel in cookielist:
        cookie = {}
        Sel = sel.split("=")
        if len(Sel) == 2:
            cookie['name'] = Sel[0]
            cookie['value'] = Sel[1]
            cookie['domain'] = '.linkedin.com'
        if len(Sel) > 2:
            cookie['name'] = Sel[0]
            cookie['value'] = '='.join(Sel[1:])
            cookie['domain'] = '.linkedin.com'
        cookies.append(cookie)
    return cookies

COOKIES = parse_cookie(cookies)
def main():
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument('proxy-server=http://localhost:8080')
    chromeOptions.add_argument('ignore-certificate-errors')
    chromeOptions.add_argument('test-type')
    chromeOptions.add_argument('disable-images')
    # chromeOptions.add_argument('user-data-dir=./chrome')
    chromeOptions.add_argument('disable-gpu') 
    # chromeOptions.add_argument('headless')
    # chromeOptions.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36')
        
    browser=webdriver.Firefox()

    browser.set_page_load_timeout(300)

    # 刷新页面
    # browser.refresh()

    browser.get('https://www.linkedin.com/')
    time.sleep(5)
    print('cookie')
    for ck in COOKIES:
        browser.add_cookie(ck)
    time.sleep(2)

   
    browser.get('https://www.linkedin.com/jobs/search/?f_L=de%3A0%2Cgb%3A0&keywords=Artificial intelligence&location=Worldwide&locationId=OTHERS.worldwide')

    for _ in range(90):
        

        browser.execute_script("$(window).scrollTop(9000)")
        time.sleep(1)
        browser.execute_script("$('.next').click()")
        time.sleep(2)
        print('click')

    # time.sleep(1000)
    # url = 'http://www.tianyancha.com/company/%s'

    # work_dic = OrderedDict()
    # browser.get('http://www.tianyancha.com')
    # # work_dic[browser.current_window_handle] = '企业热点'
    # first_head = browser.current_window_handle
    # # work_dic.append(['',browser.current_window_handle])
    # fail_dic = {}
    # print(len(t_ids))
    # while t_ids:

    #     if len(work_dic) <10:
    #         # browser.get(url%parse.quote(name))
    #         name = t_ids.pop()
    #         browser.execute_script("window.open('%s');"%url%name)
    #         time.sleep(0.5)
    #         work_dic[browser.window_handles[-1]] = name        
    #         # work_dic.append([name,browser.window_handles[-1]])
    #     else:
    #         temp_dic = work_dic.copy()
    #         for tab_id,tab_com_name in temp_dic.items():
    #             fail_dic.setdefault(tab_id,0)
    #             try:
    #                 browser.switch_to_window(tab_id)
    #                 time.sleep(0.1)
    #                 if "监控" in browser.page_source and "信用报告" in browser.page_source:
    #                     print('search ok : %s'%tab_com_name)
    #                     if t_ids:
    #                         try:
    #                             name = t_ids.pop()
    #                             browser.execute_script('location.href=%s'%url%name)
    #                             time.sleep(0.2)
    #                             work_dic[tab_id] = name  
    #                             fail_dic[tab_id] = 0
    #                         except selenium.common.exceptions.WebDriverException:
    #                             fail_dic[tab_id] += 1
    #                     else:
    #                         browser.close()
    #                         del(work_dic[tab_id])
    #                         return

    #                 else:
    #                     fail_dic[tab_id] += 1

    #                 if fail_dic[tab_id]>3 or 'An error occurred.' in browser.page_source:

    #                     browser.execute_script("window.open('%s');"%url%tab_com_name)
    #                     time.sleep(0.2)
    #                     work_dic[browser.window_handles[-1]] = tab_com_name
                        
    #                     browser.close()
    #                     del(work_dic[tab_id])
    #                     del(fail_dic[tab_id])
    #                     print('search false : %s'%tab_com_name)
    #                 elif 'from=i' in browser.current_url:
    #                     try:
    #                         browser.execute_script('location.href=%s'%url%tab_com_name)
    #                         time.sleep(0.5)
    #                         print('search angular : %s'%tab_com_name)
    #                         fail_dic[tab_id] = 0
    #                     except selenium.common.exceptions.WebDriverException:
    #                         fail_dic[tab_id] += 1
    #                     # browser.execute_script("location.reload()")
    #                     time.sleep(0.4)
    #                 elif 'antirobot' in browser.current_url:

    #                     while 'antirobot' in browser.current_url:
    #                         print("please varify")
    #                         time.sleep(20)
    #                     time.sleep(0.1)

    #                 browser.switch_to_window(first_head)
    #             except TimeoutException:
    #                 print ('time out after 30 seconds when loading page' )
    #                 # browser.execute_script('window.stop()')
    #                 browser.close()                    
    #                 del(work_dic[tab_id])
    #                 del(fail_dic[tab_id])
    #                 browser.switch_to_window(first_head)
    #                 browser.execute_script("window.open('%s');"%url%tab_com_name)
    #                 time.sleep(0.2)
    #                 work_dic[browser.window_handles[-1]] = tab_com_name
                    
    print('over')
    time.sleep(3000)
if __name__ == '__main__':

    try:
        main()
    except selenium.common.exceptions.WebDriverException as e:
        print(e)